/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package srac.controleatividades.cgt;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.zkoss.zul.Filedownload;
import srac.controleatividades.cdp.Categoria;
import srac.controleatividades.cdp.TipoAtividade;

/**
 *
 * @author gmiranda
 */
public class TabelaAtividades {
    /**
     * Gera a Tabela de Atividades em PDF
     * @param listAtv
     * @param categorias
     * @throws BadElementException
     * @throws MalformedURLException
     * @throws IOException
     * @throws DocumentException
     * @throws Exception 
     */
    public static void gerarPdf(List<TipoAtividade> listAtv, List<Categoria> categorias) throws BadElementException, MalformedURLException, IOException, DocumentException, Exception {
        Document document = new Document();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);

        

        document.open();


        Font f = new Font();
        f.setSize(14);

        Font f2 = new Font();
        f2.setSize(8);
        
        Font f3 = new Font();
        f3.setStyle(Font.BOLD);
        f3.setSize(9);
        
        Font f4 = new Font();
        f4.setStyle(Font.BOLD);
        f4.setSize(10);

        document.add(new Paragraph("\r\n"));
        Paragraph p;
        p = new Paragraph("Tabela de Atividades", f);


        p.setAlignment(Element.ALIGN_CENTER);

        document.add(p);
  
        
        document.add(new Paragraph("\r\n"));
        
        
        PdfPTable table2;
        PdfPTable table = new PdfPTable(new float[]{0.04f, 0.66f ,0.20f, 0.1f});
        table.setHorizontalAlignment(Element.ALIGN_CENTER);


        PdfPCell cell = new PdfPCell(new Paragraph("Nº", f3));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(Color.GRAY);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("Descrição da Atividade", f3));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(Color.GRAY);
        table.addCell(cell);
        

        cell = new PdfPCell(new Paragraph("Unidade de Tempo", f3));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(Color.GRAY);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("Créditos", f3));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(Color.GRAY);
        table.addCell(cell);
        
        document.add(table);
        
        Comparator c = new Comparator<TipoAtividade>(){
            @Override
            public int compare(TipoAtividade o1, TipoAtividade o2)
            {
                return (int) (o1.getCodigo()-o2.getCodigo());
            }
        };
        
        
        
        for (Categoria categoria : categorias) {
            table2 = new PdfPTable(new float[]{1f});
            cell = new PdfPCell(new Paragraph(categoria.getNome(), f4));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBackgroundColor(Color.lightGray);
            table2.addCell(cell);
            document.add(table2);
            
            List<TipoAtividade> aux = new ArrayList<TipoAtividade>();
            
            for (TipoAtividade at : listAtv) {
                if(at.getCategoria().equals(categoria))
                {
                    aux.add(at);
                    Collections.sort(aux, c);
                }
            }
            
            for (TipoAtividade at : aux) {
                table = new PdfPTable(new float[]{0.04f, 0.66f ,0.20f, 0.1f});
                cell = new PdfPCell(new Paragraph(at.getCodigo().toString(), f2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);

                table.addCell(cell);

                cell = new PdfPCell(new Paragraph(at.toString(), f2));
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(cell);


                cell = new PdfPCell(new Paragraph(at.getUnidadeTempo(), f2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

                cell = new PdfPCell(new Paragraph(at.getCreditos().toString(), f2));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

                document.add(table);
            }
        }

        
        
        
        document.add(new Paragraph("\r\n"));
        
        
        document.close();
        Filedownload fd = null;
        fd.save(baos.toByteArray(), null, "Tabela_Atividades.pdf");
        
        //TODO mandar o arquivo para cima (e lá pedir para o usuario salvar)
    }
}
